package com.isoftstone.bank.controller;

import com.isoftstone.bank.controller.base.BaseController;
import com.isoftstone.common.Page;
import com.isoftstone.common.Tidings;
import com.isoftstone.common.entity.SysUser;
import com.isoftstone.common.service.impl.SysUserRoleService;
import com.isoftstone.common.service.impl.SysUserService;
import com.isoftstone.util.*;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * Created by 文俊 on 2017/12/13.
 */
@Controller
public class UserController extends BaseController {

    @Autowired
    private SysUserService service;
    @Autowired
    private SysUserRoleService userRoleService;

    @RequiresPermissions("bms-menu.001.003")
    @RequestMapping("/bank/user")
    public ModelAndView toUserPage(){
        return new ModelAndView("/bank/user/user_list");
    }

    @RequestMapping("/bank/user/getUserlist")
    @ResponseBody
    public Object getUserlist(Page page){
        Tidings<List<SysUser>> tidings = new Tidings<>();
        PageData pd = this.getPageData();
        page.setPd(pd);
        try {
            List<SysUser> list = service.getBankUserlistPage(page);
            tidings.setT(list).setPage(page);
            tidings.setStatus(Const.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tidings;
    }

    @RequestMapping("/bank/user/editQuery")
    @ResponseBody
    public Object editQuery(String usercode){
        Tidings<SysUser> tidings = new Tidings<>();
        if(Tools.notEmpty(usercode)){
            SysUser bean = null;
            try {
                bean = service.getBankUserByUsercode(usercode);
            } catch (Exception e) {
                e.printStackTrace();
            }
            tidings.setT(bean);
        }
        tidings.setStatus(Const.SUCCESS);
        return tidings;
    }

    @RequestMapping("/bank/user/save")
    @ResponseBody
    public Object save(@ModelAttribute("role") SysUser user){
        Tidings<String> tidings = new Tidings<>();
        try {
            if (StringUtils.isEmpty(user.getUserid())) {
                user.setUserid(UuidUtil.getNumFormatUUID());
                user.setType("02");
                user.setPassword(DigestUtils.md5Hex(("a747cea3c1efabd3b5f5b60a81be59b6" + "-" + user.getUsercode())).toUpperCase());
                user.setStatus("02");
                user.setSystem("01");
                user.setCreateDate(new Date());//创建时间
                user.setCreator(this.getUser().getUserid());//创建人
                user.setLastUpdateIp(this.getRequest().getRemoteAddr());
                service.saveUser(user);
                userRoleService.updateUserRoleByUserIdAndRoleId(user.getUserid(), user.getRoleid());//更新用户与角色对应关系
                tidings.setMsg("保存成功！");
            } else {
                user.setLastUpdateIp(this.getRequest().getRemoteAddr());
                user.setLastUpdateDate(new Date());
                user.setLastUpdateCreator(this.getUser().getUserid());
                service.updateUser(user);
                userRoleService.updateUserRoleByUserIdAndRoleId(user.getUserid(), user.getRoleid());//更新用户与角色对应关系
                tidings.setMsg("修改成功！");
            }
            tidings.setStatus(Const.SUCCESS);
        } catch (Exception e) {
            tidings.setStatus(Const.ERROR);
            tidings.setMsg("保存失败！该用户账户可能已被占用");
            e.printStackTrace();
        }
        return tidings;
    }

    @RequestMapping("/bank/user/del")
    @ResponseBody
    public Object del(String usercode){
        Tidings<String> tidings = new Tidings<>();
        try {
            SysUser user = service.getBankUserByUsercode(usercode);
            service.deleteSysUserbyUserId(user.getUserid());
            userRoleService.deleteUserRole(user.getUserid());
            tidings.setStatus(Const.SUCCESS);
            tidings.setMsg("删除成功！");
        }catch (Exception e){
            tidings.setStatus(Const.SUCCESS);
            tidings.setMsg("删除失败！");
            e.printStackTrace();
        }
        return tidings;
    }
    @RequestMapping("/bank/user/changeType")
    @ResponseBody
    public Object changeType(String usercode,String status){
        Tidings<String> tidings = new Tidings<>();
        try {
            service.updateUserStatus(usercode,status);
            tidings.setStatus(Const.SUCCESS);
            tidings.setMsg("操作成功！");
        }catch (Exception e){
            tidings.setStatus(Const.SUCCESS);
            tidings.setMsg("操作成功！");
            e.printStackTrace();
        }
        return tidings;
    }

    @RequestMapping("/bank/user/initPass")
    @ResponseBody
    public Object initPass(String usercode){
        Tidings<String> tidings = new Tidings<>();
        try {
            SysUser user = service.getBankUserByUsercode(usercode);
            user.setPassword(DigestUtils.md5Hex(("a747cea3c1efabd3b5f5b60a81be59b6" + "-" + user.getUsercode())).toUpperCase());
            service.updateUser(user);
            tidings.setStatus(Const.SUCCESS);
            tidings.setMsg("操作成功！");
        }catch (Exception e){
            tidings.setStatus(Const.SUCCESS);
            tidings.setMsg("操作成功！");
            e.printStackTrace();
        }
        return tidings;
    }
}
